package com.thibal.lylo;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LyloAdapter extends BaseAdapter {
	
	private List<LyloRow> lyloRows;
	
	private final Context context;

	public LyloAdapter(Context context) {
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return lyloRows == null ? 0 : lyloRows.size();
	}

	@Override
	public Object getItem(int index) {
		return lyloRows == null ? null : lyloRows.get(index);
	}

	@Override
	public long getItemId(int index) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView;
		
		LyloRow currentRow = lyloRows.get(position);

		
		if (convertView != null) {
			rowView = convertView;
		} else {
			// Get a new instance of the row layout view
			rowView = View.inflate(context, R.layout.lylo_row, null);
		}
		
		LinearLayout layout = (LinearLayout) rowView.findViewById(R.id.secondary_layout);
		TextView time = (TextView) rowView.findViewById(R.id.textViewTime);
		TextView salle = (TextView) rowView.findViewById(R.id.textViewSalle);
		TextView band = (TextView) rowView.findViewById(R.id.textViewName);
		TextView desc = (TextView) rowView.findViewById(R.id.textViewDesc);
		TextView price = (TextView) rowView.findViewById(R.id.textViewPrice);


		ImageView style = (ImageView) rowView.findViewById(R.id.imageViewStyle);
		ImageView quarter = (ImageView) rowView.findViewById(R.id.imageViewQuarter);

		String timeText = currentRow.getTime();
		
		if (timeText.length() > 0) {
			
			time.setText(currentRow.getTime());
			time.setVisibility(View.VISIBLE);
			if(timeText.length() != 5)
			{
				time.setBackgroundColor(Color.GRAY);
				layout.setVisibility(View.GONE);
			}
			else
				layout.setVisibility(View.VISIBLE);
			
		} else {
			
			time.setVisibility(View.GONE);
		}
		
		salle.setText(currentRow.getPlaceName());
		band.setText(currentRow.getBandName());

		String descText = currentRow.getBandDescription();
		String priceText = currentRow.getPrice();
		
		if(descText.length() > 0) {
			
			desc.setText(descText);
			desc.setVisibility(View.VISIBLE);
		
		} else {
			desc.setText(R.string.no_desc_given);
			desc.setVisibility(View.VISIBLE);
		}
		
		if(priceText.length() > 0) {
			if(priceText.contains("(") && priceText.contains("€"))
			{
				String test = context.getResources().getString(R.string.buy_drink);
				price.setText(test + priceText.replace("(", "").replace(")", ""));
			}
			else if (priceText.contains("("))
			{
				String test = context.getResources().getString(R.string.free);
				price.setText(test);
			}
			else
			{
				String test = context.getResources().getString(R.string.price);
				price.setText(test + " " +priceText);
			}
			price.setVisibility(View.VISIBLE);
		
		} else {
			price.setText(R.string.no_price_given);
			price.setVisibility(View.VISIBLE);
		}

		style.setImageResource(currentRow.getStyle());
		quarter.setImageResource(currentRow.getQuarterName());

		return rowView;
	}

	public void setmLyloRowList(List<LyloRow> mLyloRowList) {
		this.lyloRows = mLyloRowList;
	}
}
